package com.GXKJ.Test.service.impl;


import com.GXKJ.Test.config.DynamicDataSource;
import com.GXKJ.Test.entity.Databasesource;
import com.GXKJ.Test.mapper.DatabasesourceMapper;
import com.GXKJ.Test.service.DatabasesourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author NongZhiQuan
 * @since 2021-05-14 10:18:55
 */
@Service
public class DatabasesourceServiceImpl implements DatabasesourceService {

    @Autowired
    private DatabasesourceMapper databasesourceMapper;

    @Autowired
    private DynamicDataSource dynamicDataSource;

    @Override
    public List<Databasesource> get() {

        return databasesourceMapper.get();
    }

    @Override
    public boolean changeDb(Integer datasourceid, String url, String dbname, String username, String password) throws Exception {
        DynamicDataSource.DBContextHolder.clearDataSource();
        List<Databasesource> databasesourceList=databasesourceMapper.get();
        for (Databasesource dataSource: databasesourceList) {
            if (dataSource.getDatasourceid().equals(datasourceid)){
                System.out.println("需要使用的的数据源已经找到,datasourceId是："+dataSource.getDatasourceid());
                //创建数据源连接&检查 若存在则不需重新创建
                try {
                    //dynamicDataSource.createDataSourceWithCheck(datasourceid,url,dbname,username,password);
                }catch (Exception e) {
                    e.printStackTrace();
                }
                //切换到该数据源
                //String id= DynamicDataSource.DBContextHolder.setDataSource(dataSource.getDatasourceid());
                //System.out.println(id);
                return true;
            }
        }
        return false;
    }


//    public boolean changeDb(String datasourceid,String url,String dbname,String username,String password) throws Exception {
//        DynamicDataSource.DBContextHolder.clearDataSource();
//        List<Databasesource> databasesourceList=databasesourceMapper.get();
//        for (Databasesource dataSource: databasesourceList) {
//            if (dataSource.getDatasourceid().equals(datasourceid)){
//                System.out.println("需要使用的的数据源已经找到,datasourceId是："+dataSource.getDatasourceid());
//                //创建数据源连接&检查 若存在则不需重新创建
//                try {
//                    dynamicDataSource.createDataSourceWithCheck(datasourceid,url,dbname,username,password);
//                }catch (Exception e) {
//                    e.printStackTrace();
//                }
//                //切换到该数据源
//                String id= DynamicDataSource.DBContextHolder.setDataSource(dataSource.getDatasourceid());
//                System.out.println(id);
//                return true;
//            }
//        }
//        return false;
//    }

    @Override
    public Databasesource selectAll(String dbname) {
        return databasesourceMapper.selectAll(dbname);
    }

    @Override
    public List<Databasesource> selectget() {
        return databasesourceMapper.selectget();
    }

    @Override
    public List selectDbTable() {
        return databasesourceMapper.selectDbTable();
    }


}





